package bll

import (
	"context"
	"maya-service/model/po"
	"maya-service/store/mysql"
	"maya-service/utils"

	"maya-service/model"
	"maya-service/model/entity"
	"maya-service/model/mapping"
	"maya-service/store"
	"time"
)

type product struct {
	iProduct store.IProduct
}

var Product = &product{
	iProduct: mysql.Product,
}

func init() {
	Register(Product)
}

func (a *product) init() func() {
	// 初始化数据
	var goods = []*entity.Product{
		{
			Name:        "文字沟通 9.9/15分钟",
			CategoryId:  1,
			ProductType: 1,
			Description: "测试商品",
			Stock:       100,
			Status:      "normal",
			ImageURL:    "",
			ExtraContext: utils.ToJsonString(po.ProductDetail{
				Name:     "文字沟通 9.9/15分钟",
				Type:     1,
				Price:    9.9,
				Duration: 15,
				Level:    1,
			}),
			CreatedAt: time.Now().Unix(),
			UpdatedAt: time.Now().Unix(),
		},
		{
			Name:        "文字沟通 9.9/15分钟",
			CategoryId:  3,
			ProductType: 2,
			Description: "测试商品",
			Stock:       100,
			Status:      "normal",
			ImageURL:    "",
			ExtraContext: utils.ToJsonString(po.ProductDetail{
				Name:     "文字沟通 9.9/15分钟",
				Type:     1,
				Price:    9.9,
				Duration: 15,
				Level:    1,
			}),
			CreatedAt: time.Now().Unix(),
			UpdatedAt: time.Now().Unix(),
		},
		{
			Name:        "文字沟通 9.9/15分钟",
			CategoryId:  3,
			ProductType: 3,
			Description: "测试商品",
			Stock:       100,
			Status:      "normal",
			ImageURL:    "",
			ExtraContext: utils.ToJsonString(po.ProductDetail{
				Name:     "文字沟通 9.9/15分钟",
				Type:     1,
				Price:    9.9,
				Duration: 15,
				Level:    1,
			}),
			CreatedAt: time.Now().Unix(),
			UpdatedAt: time.Now().Unix(),
		},
	}

	for _, v := range goods {
		_, _ = a.iProduct.Create(context.Background(), v)
	}
	return func() {}
}

// Create
func (a *product) Create(ctx context.Context, in *model.ProductCreateRequest) error {
	var (
		err error
	)
	c := buildProduct(in)
	_, err = a.iProduct.Create(ctx, c)
	return err
}

// Update
func (a *product) Update(ctx context.Context, in *model.ProductUpdateRequest) error {
	var (
		dict = make(map[string]interface{})
	)

	if in.Name != nil {
		dict["name"] = in.Name
	}

	if in.CategoryId != nil {
		dict["category_id"] = in.CategoryId
	}

	if in.ProductType != nil {
		dict["product_type"] = in.ProductType
	}

	if in.Description != nil {
		dict["description"] = in.Description
	}

	if in.Price != nil {
		dict["price"] = in.Price
	}

	if in.Stock != nil {
		dict["stock"] = in.Stock
	}

	if in.Status != nil {
		dict["status"] = in.Status
	}

	if in.ImageURL != nil {
		dict["image_u_r_l"] = in.ImageURL
	}

	if in.ExtraContext != nil {
		dict["extra_context"] = in.ExtraContext
	}

	if in.CreatedAt != nil {
		dict["created_at"] = in.CreatedAt
	}

	if in.UpdatedAt != nil {
		dict["updated_at"] = in.UpdatedAt
	}

	// do other update here
	updateAt := time.Now().Unix()
	in.UpdatedAt = &updateAt
	return a.iProduct.Update(ctx, in.Id, dict)
}

// Delete
func (a *product) Delete(ctx context.Context, in *model.ProductDeleteRequest) error {
	return a.iProduct.Delete(ctx, in.Id)
}

// List
func (a *product) List(ctx context.Context, in *model.ProductListRequest) (*model.ProductListResponse, error) {
	var (
		err   error
		total int
		list  []*entity.Product
		out   = &model.ProductListResponse{}
	)

	if total, list, err = a.iProduct.List(ctx, in); err != nil {
		return nil, err
	}

	out.Total = total
	out.List = mapping.ProductsEntityToDto(list)

	return out, nil
}

// Find
func (a *product) Find(ctx context.Context, in *model.ProductInfoRequest) (*model.ProductInfo, error) {
	var (
		err  error
		data *entity.Product
		out  = &model.ProductInfo{}
	)

	if data, err = a.iProduct.Find(ctx, in); err != nil {
		return nil, err
	}

	out = mapping.ProductEntityToDto(data)
	return out, nil
}

// buildProduct build entity
func buildProduct(in *model.ProductCreateRequest) *entity.Product {

	ety := &entity.Product{}

	ety.Name = in.Name

	ety.CategoryId = in.CategoryId

	ety.ProductType = in.ProductType

	ety.Description = in.Description

	ety.Stock = in.Stock

	ety.Status = in.Status

	ety.ImageURL = in.ImageURL

	ety.ExtraContext = in.ExtraContext

	ety.CreatedAt = time.Now().Unix()

	ety.UpdatedAt = in.UpdatedAt

	return ety
}
